package com.gxg.dao;

import com.gxg.entities.StudentUser;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

@Repository
public class StudentUserDao
{
  @Autowired
  private JdbcTemplate jdbcTemplate;
  
  public Boolean isStudentUser(String id)
  {
    int rowCount = ((Integer)this.jdbcTemplate.queryForObject("select count(*) from studentuser where studentid=?", Integer.class, new Object[] { id })).intValue();
    if (rowCount == 0) {
      return Boolean.valueOf(false);
    }
    return Boolean.valueOf(true);
  }
  
  public String insertStudentUser(StudentUser studentUser)
  {
    String sql = "insert into studentuser value(?,?,?,?,?,?,?)";
    this.jdbcTemplate.update(sql, new Object[] {studentUser
      .getStudentId(), studentUser
      .getteacherName(), studentUser
      .getTel(), studentUser
      .getClassId(), studentUser
      .getPassWord(), studentUser
      .getStudentName(), studentUser
      .getTimestamp() });
    return "添加成功！";
  }
  
  public StudentUser getStudentUserByStudentId(String studentid)
  {
    String sql = "select * from studentuser where studentid=?";
    StudentUser studentUser = (StudentUser)this.jdbcTemplate.queryForObject(sql, new RowMapper()
    {
      public StudentUser mapRow(ResultSet resultSet, int i)
        throws SQLException
      {
        StudentUser studentUser1 = new StudentUser();
        studentUser1.setteacherName(resultSet.getString("teacherName"));
        studentUser1.setStudentName(resultSet.getString("studentName"));
        studentUser1.setTel(resultSet.getString("tel"));
        studentUser1.setStudentId(resultSet.getString("studentid"));
        studentUser1.setPassWord(resultSet.getString("passwd"));
        studentUser1.setClassId(resultSet.getString("class"));
        return studentUser1;
      }
    }, new Object[] { studentid });
    
    return studentUser;
  }
  
  public List<StudentUser> getStudentUserListByTeacherName(String teacherName)
  {
    String sql = "select * from studentuser where teachername=?";
    List<StudentUser> studentUserList = this.jdbcTemplate.query(sql, new RowMapper()
    {
      public StudentUser mapRow(ResultSet rs, int rowNum)
        throws SQLException
      {
        StudentUser studentUser = new StudentUser();
        studentUser.setTimestamp(Timestamp.valueOf(rs.getString("createtime")));
        studentUser.setClassId(rs.getString("class"));
        studentUser.setPassWord(rs.getString("passwd"));
        studentUser.setteacherName(rs.getString("teachername"));
        studentUser.setStudentId(rs.getString("studentid"));
        studentUser.setTel(rs.getString("tel"));
        studentUser.setStudentName(rs.getString("studentname"));
        return studentUser;
      }
    }, new Object[] { teacherName });
    
    return studentUserList;
  }
  
  public String deleteMemberByStudentId(String studentId)
  {
    String sql = "delete from studentUser where studentId=?";
    this.jdbcTemplate.update(sql, new Object[] { studentId });
    return "ok";
  }
}
